﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using RBS.Common;
using RBS.Common.JsonEntity;
using RBS.Web.Controllers;
using Webdiyer.WebControls.Mvc;

namespace RBS.Web.Areas.User.Controllers
{
    public class CommentController : BaseController
    {
        //
        // GET: /User/Comment/

        public ActionResult CommentList(  int id = 1, string kword = null)
        {
            const int pageSize = 10;
         
            var bll = new RBS.Core.BLL.U_Comment();
            // var admin = GetAdminInfo(); //取得当前管理员信息
            if (kword != null) kword = kword.Replace("'", "");
            var strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(kword))
            {
                //strTemp.Append(" and (H_UID like  '%" + kword + "%' or H_UserName like '%" + kword + "%')");
            }
            string strWhere = strTemp.ToString();
            const string orderby = " id  desc";
            var startIndex = id;

            int totalItems = 0; //要分页的总记录数
            DataTable dt = bll.GetList(pageSize, startIndex, strWhere, orderby, out totalItems).Tables[0];
            //PagedList构造函数 
            var list = IListDataSet.DataTableToIList<RBS.Core.Model.U_Comment>(dt);
            PagedList<RBS.Core.Model.U_Comment> arts = null;
            if (list != null)
                arts = new PagedList<RBS.Core.Model.U_Comment>(list, id, pageSize, totalItems);
            else
            {
                list = new List<RBS.Core.Model.U_Comment>();
                arts = new PagedList<RBS.Core.Model.U_Comment>(list, id, pageSize, totalItems);
            }
            return View(arts);
        }
         
        public ActionResult AddComment(int userid,int hotelid)
        {  
            var model = new RBS.Core.Model.U_Comment
                {
                    UserId = userid,
                    HotelId = hotelid
                }; 
            return View(model);
        } 
        [HttpPost]
        public ActionResult AddComment(FormCollection collection)
        { 
            var a = collection;
            var model = new RBS.Core.Model.UComment
            {
                    HotelId = Convert.ToInt32(collection["HotelId"]),
                    UserId = CurrentUser.Id,
                    Comments =  collection["Comments"],
                    CommentTime =  DateTime.Now,
                    jiage =  collection["jiage"],
                    weidao =  collection["weidao"],
                    jchj =  collection["jchj"],
                    Column_8 =  collection["Column_8"],
                    Column_9 =  collection["Column_9"],
                    Column_10 =  collection["Column_10"],                    
            };

                var bll = new RBS.Core.BLL.U_Comment(); 
                bll.Add(model);
                return Content(model.HotelId.ToString()); 
        }

        [HttpPost]
        public JsonResult AjaxCommentList(int hotelId,int page=0)
        {
            var jsonData= new JsonData {Success = true};
            const int pageSize = 10; 
            var bll = new RBS.Core.BLL.U_Comment();

            string strWhere = " and HotelId=" + hotelId;
            const string orderby = " CommentTime  desc";
            var startIndex = page; 
            int totalItems = 0; //要分页的总记录数
            DataTable dt = bll.GetList(pageSize, startIndex, strWhere, orderby, out totalItems).Tables[0];
            //PagedList构造函数 
            var list = IListDataSet.DataTableToIList<RBS.Core.Model.U_Comment>(dt);
            jsonData.Data = list;

            jsonData.PageSize = pageSize;
            jsonData.TotalPage = totalItems;
            jsonData.Page = page;
            return Json(jsonData);
        }

        public ActionResult ShowCommont(int id)
        {
            return View(id);
        }

    }
}
